﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MvcBJDCWebApp.Models;

namespace MvcBJDCWebApp.Controllers
{
    public class RacesController : Controller
    {
        private APIRaceDBContext db = new APIRaceDBContext();

        // GET: Races
        public ActionResult Index()
        {
            return View(db.APIRaces.ToList());
        }

        // GET: Races/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            APIRace aPIRace = db.APIRaces.Find(id);
            if (aPIRace == null)
            {
                return HttpNotFound();
            }
            return View(aPIRace);
        }

        // GET: Races/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Races/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "APIRaceID,APIKey,saicheng,c1,c2,c3,c4T1,c4T1URL,c4R,c4T2,c4T2URL,c51,c51Link,c52,c52Link,liveid")] APIRace aPIRace)
        {
            if (ModelState.IsValid)
            {
                db.APIRaces.Add(aPIRace);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(aPIRace);
        }

        // GET: Races/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            APIRace aPIRace = db.APIRaces.Find(id);
            if (aPIRace == null)
            {
                return HttpNotFound();
            }
            return View(aPIRace);
        }

        // POST: Races/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "APIRaceID,APIKey,saicheng,c1,c2,c3,c4T1,c4T1URL,c4R,c4T2,c4T2URL,c51,c51Link,c52,c52Link,liveid")] APIRace aPIRace)
        {
            if (ModelState.IsValid)
            {
                db.Entry(aPIRace).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(aPIRace);
        }

        // GET: Races/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            APIRace aPIRace = db.APIRaces.Find(id);
            if (aPIRace == null)
            {
                return HttpNotFound();
            }
            return View(aPIRace);
        }

        // POST: Races/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            APIRace aPIRace = db.APIRaces.Find(id);
            db.APIRaces.Remove(aPIRace);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
